package com.mmdccj.yugu.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @Author: star_
 * @date: 2023-12-03 14:20
 */
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    private final StringBuilder BASEURL = new StringBuilder();

    {
        try {
            String[] urls = ResourceUtils.getURL("classpath:").getPath().split("/");

            /*
              TODO 打包上传到服务器需修改路径 开发 3
             */
            for (int i = 1; i < urls.length - 3; i++) {
                BASEURL.append(urls[i]).append("/");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 静态资源映射
     *
     * @param registry not null
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开启静态资源映射...");
        log.info("映射路径{}", BASEURL + "resource/");
        registry.addResourceHandler("/static/**").addResourceLocations("file:" + "/" + BASEURL + "resource/");
        registry.addResourceHandler("/web/**").addResourceLocations("file:" + "/" + BASEURL + "resource/web/");
        registry.addResourceHandler("/resource/**").addResourceLocations("file:" + BASEURL + "resource/");
        super.addResourceHandlers(registry);
    }
}
